package com.tongji.admin.view
{
	import com.tongji.admin.event.BrandAdminEvent;
	import com.tongji.admin.event.GoodsTypeAdminEvent;
	import com.tongji.admin.model.BrandModel;
	import com.tongji.admin.model.GoodsModel;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import org.robotlegs.mvcs.Mediator;
	
	import spark.events.IndexChangeEvent;
	
	/**
	 * 
	 * @author yanghanbing
	 * 2011/7/
	 */ 
	public class GoodsAdminMediator extends Mediator
	{
		[Inject]
		public var model:GoodsModel;
		
		[Inject]
		public var view:GoodsAdminView;
		
		[Inject]
		public var brandModel:BrandModel;
		
		override public function onRegister():void
		{			
			this.addContextListener(GoodsTypeAdminEvent.UPDATE_TYPES, updateGoodsType, GoodsTypeAdminEvent);	
			this.addContextListener(BrandAdminEvent.UPDATE_BRANDS, updateBrand, BrandAdminEvent);	
			view.addBtn.addEventListener(MouseEvent.CLICK, nextStepHandler);
		}
		
		private function nextStepHandler(event:MouseEvent):void
		{
			view.addBtn.removeEventListener(MouseEvent.CLICK, nextStepHandler);
			view.addBtn.addEventListener(MouseEvent.CLICK, addGoodsHandler);
			view.currentState = "goodsAppends";			
		}
		
		private function addGoodsHandler(event:MouseEvent):void
		{
			view.currentState = "goodsBase";
			view.addBtn.removeEventListener(MouseEvent.CLICK, addGoodsHandler);
			view.addBtn.addEventListener(MouseEvent.CLICK, nextStepHandler);			
		}
		
		private function updateGoodsType(event:GoodsTypeAdminEvent):void
		{
			view.door.dataProvider = model.doorXML;
			view.door.addEventListener(Event.CHANGE, selectGoodsType);
		}
		
		private function updateBrand(event:BrandAdminEvent):void
		{
			view.goodsBase.brandList.dataProvider = brandModel.brandArray;
			view.goodsBase.brandList.selectedIndex = 0;
		}
		
		private function selectGoodsType(event:Event):void
		{
			model.currentTypeID = (view.door.selectedItem as XML).@index.toString();
			view.goodsBase.typeName.text = (view.door.selectedItem as XML).@typeName.toString();
		}
	}
}